#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "Please try again with \"sudo $0\"." 1>&2
   exit 1
fi

PATH_CWD=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

source $PATH_CWD/.envsetup

mkdir -p $PATH_OUT

if [ ! -f $PATH_OUT_IMAGE ]; then
    echo "$PATH_OUT_IMAGE does not exist!"
    exit 1
fi

rm -rf $PATH_OUT_DUMP
echo "$PATH_OUT_DUMP is removed!"

PATH_ROOT_TEMP=$PATH_OUT/rootfs_temp
mkdir -p $PATH_ROOT_TEMP
mount -o loop $PATH_OUT/rootfs.img  $PATH_ROOT_TEMP
cp -r $PATH_ROOT_TEMP $PATH_OUT_DUMP
umount $PATH_ROOT_TEMP
rmdir $PATH_ROOT_TEMP
echo "$PATH_OUT_IMAGE has been dumped into $PATH_OUT_DUMP"